package com.nbsaas.boot;

import java.util.Arrays;

public class ParallelStreamExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};

        Arrays.stream(numbers)
                .parallel() // 将流转换为并行流
                .mapToObj(number -> calculate(number))
                .forEach(result -> System.out.println("Result: " + result));
    }

    public static int calculate(int number) {
        // 模拟耗时计算
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return number * 2;
    }
}
